﻿using System.Collections.Generic;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;

namespace MiniFox.Net.Models
{
    /// <summary>
    /// 
    /// </summary>
    public abstract class WebMessageResponseBase<T> where T : class
    {
        IDictionary<string, object> _headers;
        /// <summary>
        /// 
        /// </summary>
        public WebMessageResponseBase()
        {
            _headers = new Dictionary<string, object>();
        }
        /// <summary>
        /// 
        /// </summary>
        public MediaTypeHeaderValue ContentType
        {
            get;
            protected set;
        }

        /// <summary>
        /// 
        /// </summary>
        public IDictionary<string, object> Headers
        {
            get { return this._headers; }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="response"></param>
        protected internal Task<T> ReadResponseDataAsync(HttpResponseMessage response)
        {
            var headers = response.Headers;
            this.ContentType = response.Content.Headers.ContentType;
            foreach (var header in headers)
            {
                this.Headers.Add(header.Key, header.Value.ToString());
            }
            return ResolveHttpContentAsync(response.Content);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="httpContent"></param>
        protected abstract Task<T> ResolveHttpContentAsync(HttpContent httpContent);

    }
}
